package net.yambal.android.geo.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelperLocationLog extends SQLiteOpenHelper {
	private static final String DB_NAME = "location_log.db";
	private static final int DB_VERSION = 1;
	private static DBHelperLocationLog _my_instance = null;
	private int _m_writableDatabaseCount = 0;
	
	public DBHelperLocationLog(Context context) {
		super( context.getApplicationContext(), DB_NAME, null, DB_VERSION );
	}
	
	synchronized static
	public DBHelperLocationLog getInstance( Context context ){
		if ( _my_instance == null ){
			_my_instance = new DBHelperLocationLog(context);
		}
		return _my_instance;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		CreateMyLocationTable(db);

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}
	
	@Override
	synchronized public SQLiteDatabase getWritableDatabase(){
		SQLiteDatabase db = super.getWritableDatabase();
		if ( db != null ){
			++ _m_writableDatabaseCount;
			Log.w("db", "DBHelperLocationLog.getWritableDatabase" + _m_writableDatabaseCount);
		}
		return db;
	}
	
	private void CreateMyLocationTable(SQLiteDatabase db){
		db.execSQL("CREATE TABLE IF NOT EXISTS " + TableLocationLog.TABLE + " (" +
				"_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
				"mills INTEGER," +
				"address TEXT NOT NULL," +
				"min_count INTEGER DEFAULT 1," +
				"year INTEGER DEFAULT -1," +
				"month INTEGER DEFAULT -1," +
				"date INTEGER DEFAULT -1," +
				"day INTEGER DEFAULT 1," +
				"hour INTEGER DEFAULT 1," +
				"location_name TEXT DEFAULT NULL" +
				")");	
	}
}
